﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Ares.Engine.Lib;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Input;
using Ares.Controller;
using System.Windows.Controls;

namespace Ares.GUI.Controller
{
    public class ModalDialogController : BaseController
    {
        //The Shell handles the Modal-Dialog
        #region Modal Dialog
        private bool _hideRequest = false;

        private UIElement _parent;
        private UserControl customControl;

        public UserControl CustomControl
        {
            get
            {
                return customControl;
            }
            set
            {
                customControl = value;
                OnPropertyChanged("CustomControl");
            }
        }

        public void SetParent(UIElement parent)
        {
            _parent = parent;
        }

        private Visibility fDialogVisibility;

        public Visibility DialogVisibility
        {
            get
            {
                return fDialogVisibility;
            }
            set
            {
                fDialogVisibility = value;
                OnPropertyChanged("DialogVisibility");
            }
        }

        private Visibility fOkButtonVisibility;
        private Visibility fYesButtonVisibility;
        private Visibility fNoButtonVisibility;
        private Visibility fCancelButtonVisibility;

        public Visibility OkButtonVisibility
        {
            get
            {
                return fOkButtonVisibility;
            }
            set
            {
                fOkButtonVisibility = value;
                OnPropertyChanged("OkButtonVisibility");
            }
        }

        public Visibility YesButtonVisibility
        {
            get
            {
                return fYesButtonVisibility;
            }
            set
            {
                fYesButtonVisibility = value;
                OnPropertyChanged("YesButtonVisibility");
            }
        }

        public Visibility NoButtonVisibility
        {
            get
            {
                return fNoButtonVisibility;
            }
            set
            {
                fNoButtonVisibility = value;
                OnPropertyChanged("NoButtonVisibility");
            }
        }

        public Visibility CancelButtonVisibility
        {
            get
            {
                return fCancelButtonVisibility;
            }
            set
            {
                fCancelButtonVisibility = value;
                OnPropertyChanged("CancelButtonVisibility");
            }
        }

        #region Message

        private string fModalDialogMessageText;

        public string ModalDialogMessageText
        {
            get
            {
                return fModalDialogMessageText;
            }
            set
            {
                fModalDialogMessageText = value;
                OnPropertyChanged("ModalDialogMessageText");
            }
        }

        #region Commands
        private ICommand fOkCommand;
        private ICommand fYesCommand;
        private ICommand fNoCommand;
        private ICommand fCancelCommand;

        public ICommand OkCommand
        {
            get
            {
                if (this.fOkCommand == null)
                    this.fOkCommand = new RelayCommand(p => this.DoOk(p), () => this.CanDoOk());

                return this.fOkCommand;
            }
        }

        public ICommand YesCommand
        {
            get
            {
                if (this.fYesCommand == null)
                    this.fYesCommand = new RelayCommand(p => this.DoYes(p), () => this.CanDoYes());

                return this.fYesCommand;
            }
        }

        public ICommand NoCommand
        {
            get
            {
                if (this.fNoCommand == null)
                    this.fNoCommand = new RelayCommand(p => this.DoNo(p), () => this.CanDoNo());

                return this.fNoCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (this.fCancelCommand == null)
                    this.fCancelCommand = new RelayCommand(p => this.DoCancel(p), () => this.CanDoCancel());

                return this.fCancelCommand;
            }
        }


        private bool CanDoOk()
        {
            return true;
        }

        private void DoOk(object p)
        {
            HideHandlerDialog();
            Mediator.NotifyColleagues(Messages.DialogResponse, null);
        }

        private bool CanDoYes()
        {
            return true;
        }

        private void DoYes(object p)
        {
            
        }

        private bool CanDoNo()
        {
            return true;
        }

        private void DoNo(object p)
        {
            
        }

        private bool CanDoCancel()
        {
            return true;
        }

        private void DoCancel(object p)
        {
            
        }


        #endregion

        #endregion

        public ModalDialogController()
        {
            DialogVisibility = Visibility.Hidden;

            Mediator.Register(this, new[] { 
            
                Messages.ShowDialog

                });
        }

        private void ShowModalDialog(object args)
        {

            //Set everything according to the class
            ModalDialogMessageText = "Text";
            CustomControl = (UserControl)args;
 
            DialogVisibility = Visibility.Visible;

            _hideRequest = false;
            while (!_hideRequest)
            {
                try
                {
                    // HACK: Stop the thread if the application is about to close
                    if (Application.Current == null ||
                        Application.Current.Dispatcher.HasShutdownStarted ||
                        Application.Current.Dispatcher.HasShutdownFinished)
                    {
                        break;
                    }

                    // HACK: Simulate "DoEvents"
                    Application.Current.Dispatcher.Invoke(
                        DispatcherPriority.Background,
                        new ThreadStart(delegate { }));
                    Thread.Sleep(20);
                }
                catch
                {

                }

            }
        }


        private void HideHandlerDialog()
        {
            _hideRequest = true;
            DialogVisibility = Visibility.Hidden;
        }
        #endregion

        public override void MessageNotification(string message, object args)
        {
            switch (message)
            {
                case Messages.ShowDialog:
                    //Needs a corresponding object
                    if (args != null)
                    {
                        ShowModalDialog(args);
                    }
                    break;
            }
        }
    }
}
